package com.xiang.sql.entity;

import java.io.Serializable;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiang.api.Util;
import com.xiang.api.entity.PermissionValue;
import com.xiang.sql.StaticSql;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户 数据实体
 * 
 * @author mac-xiang
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName(value = "`user`", autoResultMap = true)
@ApiModel(value = "User对象", description = "用户")
public class User implements Serializable {

  private static final long serialVersionUID = 1L;

  @TableId(value = "id", type = IdType.ASSIGN_ID)
  public String id;

  /** 创建时间 */
  @TableField(value = "`create_time`", fill = FieldFill.INSERT)
  public Long createTime;

  /** 类型 */
  @TableField(value = "`gid`", fill = FieldFill.INSERT)
  public String gid;

  /** 上级id */
  @TableField(value = "`pid`", fill = FieldFill.INSERT)
  public String pid;

  /** 余额 */
  @TableField(value = "`money`", fill = FieldFill.INSERT)
  public Long money;

  /** 微信id */
  @TableField(value = "`wxid`", fill = FieldFill.INSERT)
  public String wxid;

  /** 状态 */
  @TableField(value = "`state`", fill = FieldFill.INSERT)
  public Integer state;

  /** 拥有权限 */
  @TableField(value = "`permission`")
  public byte[] permission;

  /** 用户名 */
  @TableField(value = "`username`")
  public String username;

  /** 密码 */
  @TableField(value = "`password`", select = false)
  public String password;

  /** 名称 */
  @TableField(value = "`name`", fill = FieldFill.INSERT)
  public String name;

  /** 提成百分比 */
  @TableField(value = "`commission`")
  public Integer commission;

  /** 令牌 */
  @TableField(value = "`token`", fill = FieldFill.INSERT)
  public String token;

  /** 其他信息 */
  @TableField(value = "`other`")
  public String other;

  /** 用户等级 */
  @TableField(value = "live", exist = false)
  public Integer live;

  public int gLive() {
    if (live == null) {
      live = Util.isEmpty(id) ? 0 : StaticSql.service.user.getLive(id);
    }
    return live;
  }

  public User() {
  }

  public User(Boolean system) {
    if (system != null && system) {
      permission = new byte[255];
      for (int i = 0; i < permission.length; i++) {
        permission[i] = -1;
      }
      id = "0";
    }
  }

  public boolean per(String perName) {
    if (Util.pd == null) {
      return false;
    }
    PermissionValue pv = null;
    try {
      pv = Util.pd.getObject(perName, PermissionValue.class);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return pv != null && pv.i >= 0 && pv.i < this.permission.length && (this.permission[pv.i] & pv.v) != 0;
  }

  public void createValidator() {
    if (permission == null || permission.length != 20) {
      // throw new RuntimeException("权限字段数据必须为长度20");
      permission = new byte[20];
    }
    if (username == null) {
      // throw new RuntimeException("用户名 不能为空");
      if (wxid == null) {
        throw new RuntimeException("用户名 不能为空");
      } else {
        username = Util.sGetId();
      }
    }
    if (username.length() < 4) {
      throw new RuntimeException("用户名长度不能小于4");
    }
    if (username.length() > 20) {
      throw new RuntimeException("用户名长度不能大于20");
    }
    QueryWrapper<User> q = new QueryWrapper<User>().eq("username", username);
    if (StaticSql.service.user.count(q) > 0) {
      throw new RuntimeException("用户名重复");
    }
    if (password == null) {
      if (wxid == null) {
        throw new RuntimeException("密码 不能为空");
      } else {
        password = Util.sGetId();
      }
    }
    if (name == null) {
      if (wxid == null) {
        throw new RuntimeException("昵称 不能为空");
      } else {
        name = "用户" + Util.sGetId();
      }
    }
    if (other == null) {
      other = "{}";
    }
  }
}
